<template>
  <div class="main-layout">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-container">
        <!-- Logo -->
        <div class="logo" @click="$router.push('/')">
          <img src="/favicon.ico" alt="剪纸艺术平台" />
          <span class="logo-text">剪纸艺术平台</span>
        </div>

        <!-- 主导航 -->
        <nav class="main-nav">
          <router-link to="/" class="nav-item" active-class="active">首页</router-link>
          <router-link to="/culture" class="nav-item" active-class="active">剪纸文化</router-link>
          <router-link to="/tutorial" class="nav-item" active-class="active">教程学习</router-link>
          <router-link to="/news" class="nav-item" active-class="active">活动资讯</router-link>
          <router-link to="/ai-assistant" class="nav-item" active-class="active">AI助手</router-link>
        </nav>

        <!-- 搜索框 -->
        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索内容..."
            @keyup.enter="handleSearch"
            clearable
          >
            <template #append>
              <el-button @click="handleSearch" :icon="Search" />
            </template>
          </el-input>
        </div>

        <!-- 用户操作区 -->
        <div class="user-actions">
          <template v-if="userStore.isLoggedIn">
            <!-- 已登录 -->
            <el-dropdown @command="handleUserCommand">
              <div class="user-info">
                <el-avatar :src="userStore.userAvatar" :size="32">
                  <User />
                </el-avatar>
                <span class="username">{{ userStore.userName }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="favorites">我的收藏</el-dropdown-item>
                  <el-dropdown-item command="study-records">学习记录</el-dropdown-item>
                  <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <!-- 未登录 -->
            <div class="auth-buttons">
              <el-button @click="$router.push('/login')" type="primary">登录</el-button>
              <el-button @click="$router.push('/register')" plain>注册</el-button>
            </div>
          </template>
        </div>
      </div>
    </header>

    <!-- 主要内容区 -->
    <main class="main-content">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>

    <!-- 底部 -->
    <footer class="footer">
      <div class="footer-container">
        <div class="footer-content">
          <div class="footer-section">
            <h4>关于我们</h4>
            <p>传承中华剪纸艺术，弘扬传统文化精神</p>
          </div>
          <div class="footer-section">
            <h4>快速导航</h4>
            <div class="footer-links">
              <router-link to="/culture">剪纸文化</router-link>
              <router-link to="/tutorial">教程学习</router-link>
              <router-link to="/news">活动资讯</router-link>
            </div>
          </div>
          <div class="footer-section">
            <h4>联系我们</h4>
            <p>邮箱: contact@papercutting.com</p>
            <p>电话: 400-123-4567</p>
          </div>
        </div>
        <div class="footer-bottom">
          <p>&copy; 2024 剪纸艺术平台. All rights reserved.</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, User, ArrowDown } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const searchKeyword = ref('')

// 处理搜索
const handleSearch = () => {
  if (!searchKeyword.value.trim()) {
    ElMessage.warning('请输入搜索关键词')
    return
  }
  router.push({
    path: '/search',
    query: { keyword: searchKeyword.value.trim() }
  })
}

// 处理用户下拉菜单命令
const handleUserCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'favorites':
      router.push('/favorites')
      break
    case 'study-records':
      router.push('/study-records')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 处理退出登录
const handleLogout = async () => {
  try {
    await userStore.logout()
    ElMessage.success('退出登录成功')
    router.push('/')
  } catch (error) {
    ElMessage.error('退出登录失败')
  }
}

onMounted(() => {
  // 初始化用户信息
  userStore.init()
})
</script>

<style scoped>
.main-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 头部样式 */
.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  height: 64px;
}

.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
  margin-right: 40px;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 8px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: #d32f2f;
}

.main-nav {
  display: flex;
  margin-right: auto;
}

.nav-item {
  padding: 0 20px;
  line-height: 64px;
  text-decoration: none;
  color: #333;
  transition: color 0.3s;
}

.nav-item:hover,
.nav-item.active {
  color: #d32f2f;
}

.search-box {
  width: 300px;
  margin-right: 20px;
}

.user-actions {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.username {
  margin: 0 8px;
  font-size: 14px;
}

.auth-buttons .el-button + .el-button {
  margin-left: 8px;
}

/* 主内容样式 */
.main-content {
  flex: 1;
  min-height: calc(100vh - 64px - 200px);
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 底部样式 */
.footer {
  background: #2c3e50;
  color: #ecf0f1;
  margin-top: auto;
}

.footer-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px 20px;
}

.footer-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
  margin-bottom: 30px;
}

.footer-section h4 {
  font-size: 18px;
  margin-bottom: 15px;
  color: #d32f2f;
}

.footer-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.footer-links a {
  color: #bdc3c7;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-links a:hover {
  color: #d32f2f;
}

.footer-bottom {
  border-top: 1px solid #34495e;
  padding-top: 20px;
  text-align: center;
  color: #95a5a6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    flex-wrap: wrap;
    height: auto;
    padding: 10px 20px;
  }

  .logo {
    margin-right: 20px;
  }

  .main-nav {
    order: 3;
    width: 100%;
    justify-content: center;
    margin-right: 0;
    border-top: 1px solid #f0f0f0;
    margin-top: 10px;
  }

  .nav-item {
    padding: 0 15px;
    line-height: 48px;
  }

  .search-box {
    width: 200px;
    margin-right: 15px;
  }

  .footer-content {
    grid-template-columns: 1fr;
    text-align: center;
  }
}
</style>
